import java.awt.Color;

import javax.sound.midi.Sequence;

public class Producer implements Runnable {

	Thread thread;
	int currentNote, targetNote;
	int currentDuration, targetDuration;
	Sequence sequence;
	boolean produce_more;
	
	
	public Producer() {
		
		
		
	
		currentNote= 60;
		targetNote = 60;
		thread = new Thread(this, "PRODUCER_THREAD");
		System.out.println("New thread: " + thread);
		produce_more= true;

	}

	public void startThread() {
		thread.start();// Start the thread
		System.out.println("started thread: " + thread);

		produce_more = true;

	}

	
	
	// Dummy function to generate a series of notes.
	public int getNextNote() {
		
		targetNote=Global.freehand.getTargetNote();
		
		System.out.println("target note is >>> "+targetNote);
		
		if (currentNote > targetNote) {
			do{
				currentNote = currentNote  - 1;
				System.out.println(currentNote+" **decrease**");
			}while(!Global.raga.arohan.contains((Integer)(currentNote)));
			
		} else if (currentNote < targetNote) {
			do{
				currentNote = currentNote  + 1;
				System.out.println(currentNote+" **inclrese**");
			}while(!Global.raga.avrohan.contains((Integer)(currentNote)));
		}
	
		return currentNote;
		
	}
	public int getNextDuration()
	{
		
		targetDuration= Global.freehand.getTargetDuration();
		/*
		if (currentDuration > targetDuration) {
			currentDuration = currentDuration - 100;
		} else if (currentDuration <targetDuration) {
			currentDuration = currentDuration + 100;
		}
		return currentDuration;
		*/
		return targetDuration;
	}

	
	@Override
	public synchronized void run() {
		
		int temp_note;
		int temp_duration;
		int count=0;
		while(this.produce_more!=false)
		{
	
			if(Global.musicBuffer.list.size()<1)
			{
			System.out.println("in run of producer");
			temp_note = getNextNote();
			temp_duration= getNextDuration();
			Global.musicBuffer.pushEvent(1, temp_note+Global.base-12, 126, 0,temp_duration);
			
			count++;
			}
		} 
		
		Global.musicBuffer.pushEvent(-1, 0, 0, 0,0);
		System.out.println("out of producer run");

	}

}
